<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <input id="id" name="id" type="hidden" value="-1">
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input readonly id="username" type="text" name="username" lay-verify="required" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">套餐</label>
            <div class="layui-input-inline">
                <select id="packages" name="packageid">
                    <option value="">请选择套餐</option>

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;
        let order = JSON.parse(localStorage.getItem('order'))
        $.ajax({
            url: '/mobile-package/list',
            method: 'get',
            dataType: 'json',
            success: function (resp) {
                let list = resp.data
                let op = list.map(item => {
                    if (item.id == order.packageid) {
                        return `<option value="${item.id}" selected>${item.name}</option>`
                    } else {
                        return `<option value="${item.id}">${item.name}</option>`
                    }
                }).join('')
                console.log(op)
                $('#packages').append(op)
                layui.form.render()
            }
        })

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();
        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;
        $('#id').val(order.id)
        $('#username').val(order.username)
        localStorage.removeItem('order')
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url: '../order/update',
                method: "POST",
                dataType: 'json',
                data: data.field,
                success: function (resp) {
                    var index = layer.alert('保存成功', function () {
                        // 关闭弹出层
                        layer.close(index);
                        layer.close(parentIndex);
                        window.location.reload();
                    });
                }
            })
            return false;
        });

    });
</script>